import { handleFactory, handleTypeEnum } from "../handleFactory";
/**
 * 响应拦截器工厂
 *
 * 根据策略获取处理器工厂方法
 *
 */
class InterceptorFactory {
  #strategie;
  constructor() {
    const codeMap = new Map();
    codeMap.set((code) => code < 1000, handleTypeEnum.ERROR);
    codeMap.set((code) => code >= 1000 && code < 2000, handleTypeEnum.SUCCESS);
    codeMap.set((code) => code >= 2000 && code <= 4000, handleTypeEnum.EXPCEPTION);
    codeMap.set((code) => code === 4001, handleTypeEnum.LOGIN_EXPIRE);
    codeMap.set((code) => code === 4002, handleTypeEnum.TOKEN_EXPIRE);
    codeMap.set((code) => code > 4002, handleTypeEnum.EXPCEPTION);
    this.#strategie = codeMap;
  }
  #getStrategie(code) {
    for (const [key, value] of this.#strategie) {
      if (key(code)) {
        return value;
      }
    }
    return handleTypeEnum.DEFAULT;
  }
  execute(http, res) {
    const strategie = this.#getStrategie(res.data.code);
    return handleFactory.execute(strategie, http, res);
  }
}

const interceptorFactory = new InterceptorFactory();
export default interceptorFactory;
